﻿namespace SLClient.ViewModels
{
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using System;
    using System.Linq.Expressions;
    using System.Net.NetworkInformation;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Threading;

    public class OnlineViewModel : ViewModelBase
    {
        private Visibility _canVisibility;
        private string _content;
        private readonly DispatcherTimer _dispatchTimer;
        private SolidColorBrush _fillColor;
        private Uri _hyperlinkUri;
        private string _showMsg;
        public static int connectTimes;

        private void connectInfo(object o, EventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                this.ShowMsg = "连接正常";
                this.FillColor = new SolidColorBrush(Color.FromArgb(0xff, 0, 0xff, 0));
                connectTimes = 0;
            }
            else
            {
                connectTimes = 0;
                this.ShowMsg = string.Format("Reconnect {0} time", connectTimes);
                this.FillColor = new SolidColorBrush(Colors.Red);
                this._dispatchTimer.Interval=new TimeSpan(0, 0, 0, 8);
                this._dispatchTimer.Tick+=new EventHandler(this.DispatchTimerTick);
                this._dispatchTimer.Start();
            }
        }


        public OnlineViewModel()
        {
            NetworkAddressChangedEventHandler handler = null;
            this._dispatchTimer = new DispatcherTimer();
            this._showMsg = "连接正常";
            this._content = "在线客服";
            this._canVisibility = Visibility.Collapsed;
            this._fillColor = new SolidColorBrush(Color.FromArgb(0xff, 0, 0xff, 0));
            this.ShowHyperlinker();
            if (handler == null)
            {
                handler = new NetworkAddressChangedEventHandler(connectInfo);
            }
            NetworkChange.NetworkAddressChanged+=new NetworkAddressChangedEventHandler(handler);
        }

        private void DispatchTimerTick(object sender, EventArgs e)
        {
            this.ShowMsg = string.Format("Reconnect {0} times", connectTimes);
            if (connectTimes == 5)
            {
                this._dispatchTimer.Stop();
            }
            connectTimes++;
        }

        private void ShowHyperlinker()
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetAllCustomerServiceCompleted += delegate (object obj, GetAllCustomerServiceCompletedEventArgs arg) {
                if (arg.Result != null)
                {
                    if (!string.IsNullOrEmpty(arg.Result.Name))
                    {
                        this.Content = arg.Result.Name;
                    }
                    if (!string.IsNullOrEmpty(arg.Result.Url))
                    {
                        this.HyperlinkUri = new Uri(arg.Result.Url, UriKind.Absolute);
                        this.CanVisibility = 0;
                    }
                }
            };
            instance.GetAllCustomerServiceAsync();
        }

        public Visibility CanVisibility
        {
            get
            {
                return this._canVisibility;
            }
            set
            {
                this._canVisibility = value;
                base.Notify<Visibility>(() => this.CanVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(OnlineViewModel)), (MethodInfo)(OnlineViewModel.get_CanVisibility)), new ParameterExpression[0]));
            }
        }

        public string Content
        {
            get
            {
                return this._content;
            }
            set
            {
                this._content = value;
                base.Notify<string>(() => this.Content);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(OnlineViewModel)), (MethodInfo)(OnlineViewModel.get_Content)), new ParameterExpression[0]));
            }
        }

        public SolidColorBrush FillColor
        {
            get
            {
                return this._fillColor;
            }
            set
            {
                this._fillColor = value;
                base.Notify<SolidColorBrush>(() => this.FillColor);
                //base.Notify<SolidColorBrush>(System.Linq.Expressions.Expression.Lambda<Func<SolidColorBrush>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(OnlineViewModel)), (MethodInfo)(OnlineViewModel.get_FillColor)), new ParameterExpression[0]));
            }
        }

        public Uri HyperlinkUri
        {
            get
            {
                return this._hyperlinkUri;
            }
            set
            {
                this._hyperlinkUri = value;
                base.Notify<Uri>(() => this.HyperlinkUri);
                //base.Notify<Uri>(System.Linq.Expressions.Expression.Lambda<Func<Uri>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(OnlineViewModel)), (MethodInfo)(OnlineViewModel.get_HyperlinkUri)), new ParameterExpression[0]));
            }
        }

        public string ShowMsg
        {
            get
            {
                return this._showMsg;
            }
            set
            {
                this._showMsg = value;
                base.Notify<string>(() => this.ShowMsg);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(OnlineViewModel)), (MethodInfo)(OnlineViewModel.get_ShowMsg)), new ParameterExpression[0]));
            }
        }
    }
}

